package ru.geyes.screw.collections;

import java.util.Map;

/**
 * @author alex
 *         date 31.05.2008
 *         time 17:29:48
 */
public class TypedHashMap<K, V> extends HashMapX<K, V> {
    private Class<K> keyClass;
    private Class<V> valueClass;

    public TypedHashMap(Map<? extends K, ? extends V> m, Class<K> key, Class<V> value) {
        super(m);
        initClasses(key, value);
    }

    public TypedHashMap(int initialCapacity, float loadFactor, Class<K> key, Class<V> value) {
        super(initialCapacity, loadFactor);
        initClasses(key, value);
    }

    public TypedHashMap(int initialCapacity, Class<K> key, Class<V> value) {
        super(initialCapacity);
        initClasses(key, value);
    }

    public TypedHashMap(Class<K> key, Class<V> value) {
        super();
        initClasses(key, value);
    }

    private void initClasses(Class<K> key, Class<V> value) {
        this.keyClass = key;
        this.valueClass = value;
    }


    public V get(Object key) {
        if (key != null && !keyClass.isAssignableFrom(key.getClass()))
            throw new IllegalArgumentException("Class of your key object is not assignable with " + keyClass); 
        return super.get(key);
    }

    public V put(K key, V value) {
        if (key != null && !keyClass.isAssignableFrom(key.getClass()))
            throw new IllegalArgumentException("Class of your key object [" + key.getClass() + "] is not assignable with " + keyClass);
        if (value != null && !valueClass.isAssignableFrom(value.getClass()))
            throw new IllegalArgumentException("Class of your value object [" + value.getClass() + "] is noy assignable with " + valueClass);
        return super.put(key, value);
    }
}
